package com.scqkxx.gamebox.activity.group;

import android.util.Log;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.FunctionCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.databinding.ActivityGroupInviteCodeBinding;
import com.scqkxx.gamebox.databinding.ActivityJoinGroupBinding;
import com.scqkzqtz.base.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

public class GroupInviteCodeActivity extends BaseActivity<ActivityGroupInviteCodeBinding> {

    @Override
    public int setLayoutId() {
        return R.layout.activity_group_invite_code;
    }

    @Override
    public void init() {
        binding.titleView.setOnTitleClickListener(mTitleType -> finish());

        String groupId = getIntent().getStringExtra("groupId");
        createInviteCode(groupId);
        binding.btProduceCode.setOnClickListener(view -> createInviteCode(groupId));
    }

    private void createInviteCode(String groupId){
        Map<String,Object> map = new HashMap<>();
        map.put("groupId",Integer.parseInt(groupId));
        AVCloud.callFunctionInBackground("createInviteCode",map,new FunctionCallback<Map<String,Object>>(){
            @Override
            public void done(Map<String,Object> result, AVException e) {
                Log.i("createInviteCode",result+"");//{msg=邀请码创建成功, data={checkCode=7726}, error_code=0}
                if(e == null){
                    if("0".equals(result.get("error_code")+"")){
                        Map<String,Object> data = (Map<String, Object>) result.get("data");
                        binding.tvCode.setText(data.get("checkCode")+"");
                    }
                }
            }
        });
    }
}
